﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using OpenLS.Spreadsheet.Drawing;

namespace OpenLS.Spreadsheet.Formats.Biff.OfficeArt
{
    internal class OfficeArtSpgrContainer : OfficeArtSpgrContainerFileBlock
    {
        readonly List<OfficeArtSpgrContainerFileBlock> rgfb = new List<OfficeArtSpgrContainerFileBlock>();
        public override RecType RecordType
        {
            get { return RecType.OfficeArtSpgrContainer; }
        }
        

        protected override void ReadNoHeader(OfficeArtReadContext context)
        {
            Contract.Assert(rhrecVer == 0xF);
            Contract.Assert(rhrecInstance == 0);

            OfficeArtSpgrContainerFileBlock item = OfficeArtSpgrContainerFileBlock.ReadOptional(context);
            rgfb.Clear();
            while (item !=null)
            {
                rgfb.Add(item);
                item = OfficeArtSpgrContainerFileBlock.ReadOptional(context);
            }
        }

        protected override byte Version
        {
            get { return 0xF; }
        }

        protected override int ByteLength
        {
            get { return Enumerable.Sum(this.rgfb, item => item.FullByteLength); }
        }

        public List<OfficeArtSpgrContainerFileBlock> Shapes
        {
            get { return this.rgfb;}
        }


        protected override void WriteNoHeader(OfficeArtWriteContext context)
        {
            Contract.Assert(rhrecVer == 0xF);
            Contract.Assert(rhrecInstance == 0);
            foreach (var item in rgfb)
                item.Write(context);
        }


        public override Anchor GetClientAnchorSheet(BiffReaderWorksheetContext context)
        {
            return null;
        }

        public override uint SpId
        {
            get { throw new NotImplementedException(); }
        }


        public void AddShape(OfficeArtSpContainer item)
        {
            Contract.Requires(item != null);
            this.rgfb.Add(item);
        }
    }
}